home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / SAT 2.3.7 / Demos / Demo ƒ / StepPlatform Demo ƒ / sHMovPlatform.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-05  |  2.8 KB  |  109 lines  |  [TEXT/KAHL]

  1. /*****************************************
  2. *    Platform sprite, moveable version, not faceless        *
  3. /****************************************/
  4.  
  5. #include"SAT.h"
  6. #include "myPlatform.h"
  7.  
  8. #ifndef abs
  9. #define abs(x)    (x>0?x:-x)
  10. #endif
  11.  
  12. /*************** In platformSAT.h *****************\
  13. *    #define    MaxV(me)        (*(MovPlatP)me->appPtr).MaxV
  14. *    #define    MinV(me)        (*(MovPlatP)me->appPtr).MinV
  15. *
  16. *    typedef struct {
  17. *            short    MaxV,MinV;
  18. *            short    MaxX,MinX;
  19. *            short    filled;    //unused
  20. *            } MovPlatRec,*MovPlatP;
  21. *********************************************/    
  22.  
  23. extern FacePtr    platFace; /* Global from sMovPlatform.c */
  24.  
  25. void InitHMovPlatform()
  26. {
  27. }
  28.  
  29. pascal void SetupHMovPlatform(SpritePtr me)
  30. {
  31.     Rect            r;
  32.     PolyHandle    pol;
  33.     
  34.     me->speed.h = -1 + SATRand(2) * 2;
  35.     me->face = platFace; 
  36.     me->appPtr =  NewPtr(sizeof(MovPlatRec));
  37.     if (me->appPtr)        {
  38.         (*(MovPlatP)me->appPtr).MaxH =me->position.h;
  39.         (*(MovPlatP)me->appPtr).MinH =me->kind;
  40.     }
  41.     SetRect(&(me->hotRect), 0, 3, 60, 20);
  42.     me->task = &HandleHMovPlatform;
  43.     me->hitTask = &HitHMovPlatform;
  44. }
  45.  
  46. pascal void HandleHMovPlatform(SpritePtr me)
  47. {
  48.     me->position.h = me->position.h + me->speed.h;
  49.     if(me->position.h < MinH(me))    me->speed.h = abs(me->speed.h);
  50.     if(me->position.h > MaxH(me))    me->speed.h = -abs(me->speed.h);
  51.  
  52.     if(me->speed.h == 0){
  53.         if(me->position.h > (MaxH(me)-MinH(me)) / 2)
  54.             me->speed.h = -abs(me->speed.h);
  55.         else
  56.             me->speed.h = abs(me->speed.h);
  57.     }
  58.     me->layer = -me->position.v;
  59. }
  60.  
  61. pascal void HitHMovPlatform(SpritePtr me, PlSpritePtr him)
  62. {
  63.     int     mini, i, min;
  64.     int    diff[5];
  65.     
  66.     if (him->task == HandleMovPlatform) {
  67.         SATReportStr("\pThere Has been a collotion between HmovPlatform and MovPlatform you shpuld fix that");
  68.         }
  69.     if(him->task == HandlePlayerSprite) {
  70.         diff[1] = -me->hotRect2.top + (him->hotRect2.bottom);            /* TtoB */
  71.         diff[2] = -him->hotRect2.top + (me->hotRect2.bottom);            /* BtoT */
  72.         diff[3] = -me->hotRect2.left + (him->hotRect2.right);            /* LtoR */
  73.         diff[4] = -him->hotRect2.left + (me->hotRect2.right);            /* RtoL */
  74.         mini = 0;
  75.         min = 10000;
  76.         for(i = 1; i <= 4; i++)
  77.             if(min > diff[i]){
  78.                     min = diff[i];
  79.                     mini = i;
  80.             }
  81.         switch(mini){
  82.             case 1: /*floor*/
  83.                     him->action = StandOnHMovPlatform;
  84.                     him->speed.h = me->speed.h;
  85.                     him->position.v = him->position.v - diff[1] + 1;
  86.                     him->position.h = him->position.h + me->speed.h; 
  87.                     if(him->speed.v > 0)
  88.                         him->speed.v = 0;
  89.                     break;
  90.             case 2: /* ceiling */
  91.                     him->position.v = him->position.v + diff[2] + 1;
  92.                     if(him->speed.v < 0)
  93.                         him->speed.v = -him->speed.v;
  94.                     break;
  95.             case 3: /*left*/
  96.                     him->position.h = him->position.h - diff[3] - 1;
  97.                     if (him->speed.h > 0)    
  98.                         him->speed.h = -him->speed.h;
  99.                     break;
  100.             case 4: /*right*/
  101.                     him->position.h = him->position.h + diff[4] + 1;
  102.                     if (him->speed.h < 0)    
  103.                         him->speed.h = -him->speed.h;
  104.                     break;
  105.         } /* switch */
  106.     } /* if */
  107.     
  108. }
  109.